package com.zg.paging3.activity.java;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.paging.PagingDataAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.zg.paging3.R;
import com.zg.paging3.activity.java.model.ArticleBeanInJava;

import org.jetbrains.annotations.NotNull;

import kotlinx.coroutines.CoroutineDispatcher;

/***
 * @Author: zxd
 * @CreateDate: 2021/7/9 17:22
 * @Description:
 */
public class MyAdapter extends PagingDataAdapter<ArticleBeanInJava, MyAdapter.ViewHolder> {

    public MyAdapter(@NotNull CoroutineDispatcher mainDispatcher, @NotNull CoroutineDispatcher workerDispatcher) {
        super(new ArticleDiffCallback(), mainDispatcher, workerDispatcher);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.textView.setText(getItem(position).getTitle());
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv);
        }
    }
}
